package it.polimi.traveldream.web.beans;

import java.util.List;





import it.polimi.traveldream.ejb.management.VoloDTO;
import it.polimi.traveldream.ejb.management.VoloMgr;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;

@ManagedBean(name = "voloBean")
@RequestScoped
public class BeanVolo {
	
	@EJB
	private VoloMgr voloMgr;
	
	private VoloDTO volo;
	
	private VoloDTO selected;
	
	@PostConstruct
	void init() {
		volo = new VoloDTO();
	}
	
	
	public VoloDTO getVolo(){
	return this.volo;
	}
	
	public void setVolo(VoloDTO volo) {
		this.volo = volo;
	}
	
	public String creaVolo(){
		if(volo.getAeroportoArrivo().equals(volo.getAeroportoPartenza())){
			return "/pagine_impiegati/erroreVolo?faces-redirect=true";
		}
		else{
			voloMgr.save(volo);
			return "/pagine_impiegati/prodottoOk.xhtml?faces-redirect=true";
		}
	}
	
	public List<VoloDTO> getListaVoli() {
		return voloMgr.getListaVoli();
	}
	
	public String cancella(){
		voloMgr.elimina(volo);
		return "/pagine_impiegati/gestioneVoli?faces-redirect=true";
	}
	
	public void confirmDel(){
		cancella();
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Cancellazione completata", "");  

		FacesContext.getCurrentInstance().addMessage(null, message);
	}

	public String viewVoli(){
		return "/pagine_impiegati/gestioneVoli?faces-redirect=true";
	}


	public VoloDTO getSelected() {
		return selected;
	}


	public void setSelected(VoloDTO selected) {
		this.selected = selected;
	}

}
